const fs = require('fs');
const path = require('path');

const articleSchema = require('../../schemas/articleSchema.js');//文章表

function GetBanner(){
	let result = fs.readFileSync('./data/requiredJSON/banner.json');
	let banners = JSON.parse(result.toString());
	return banners;
}
function GetSpicalColumn(){
	let result = fs.readFileSync('./data/requiredJSON/spicalColumn.json');
	let spiclaColumns = JSON.parse(result.toString());
	return spiclaColumns;
}
function GetRecommend(){
	let result = fs.readFileSync('./data/requiredJSON/recommends.json');
	let recommends = JSON.parse(result.toString());
	return recommends;
}
function GetFriendLinks(){
	let result = fs.readFileSync('./data/requiredJSON/friendLinks.json');
	let friendLinks = JSON.parse(result.toString());
	return friendLinks;
}
function GetFooterInfo(){
	let result = fs.readFileSync('./data/requiredJSON/connectInfo.json');
	let footerInfo = JSON.parse(result.toString());
	return footerInfo;
}
function GetSeoInfo(){
	let info = fs.readFileSync('./data/requiredJSON/seoInfo.json');
	info = JSON.parse(info.toString());
	return info;
}

//获取banner
exports.getBanner = async ctx=>{
	let banners = GetBanner();
	ctx.body ={
		data:banners
	}
};
//修改banner
exports.changeBanner = async ctx=>{
	let {data} = ctx.request.body;
	fs.writeFileSync('./data/requiredJSON/banner.json',JSON.stringify(data));
	ctx.body = {
		status:true
	}
};
//获取专栏信息
exports.getSpicalColumn = async ctx=>{
	let spiclaColumns = GetSpicalColumn();
	ctx.body = {
		data:spiclaColumns
	}
};
//修改专栏信息
exports.changeSpicalColumn = async ctx=>{
	let {name1,name2,id1,id2,bg1,bg2,alt1,alt2,title1,title2} = ctx.request.body;
	let data = [
		{
			id:id1,
			name:name1,
			imgUrl:bg1,
			alt:alt1,
			title:title1
		},
		{
			id:id2,
			name:name2,
			imgUrl:bg2,
			alt:alt2,
			title:title2
		}
	];
	fs.writeFileSync('./data/requiredJSON/spicalColumn.json',JSON.stringify(data));
	ctx.body = {
		status:true
	}
};
//获取作者信息
exports.getAuthorInfo = async ctx=>{
	let data = fs.readFileSync('./data/requiredJSON/authorInfo.json');
	data = JSON.parse(data);
	ctx.body = {
		data
	}
};
//修改作者信息
exports.changeAuthorInfo = async ctx=>{
	let {name,imgUrl,alt,title,description} = ctx.request.body;
	let data = {name,imgUrl,alt,title,description};
	fs.writeFileSync('./data/requiredJSON/authorInfo.json',JSON.stringify(data));
	ctx.body = {
		status:true
	}
};
//获取推荐文章
exports.getRecommend = async ctx=>{
	let data = GetRecommend();
	ctx.body = {
		data
	}
};
//修改推荐文章
exports.changeRecommend = async ctx=>{
	let {index,id} = ctx.request.body;
	let recommends = GetRecommend();
	let result = await articleSchema.findOne({id});
	if(result){
		let {title,imgUrl,imgAlt,imgTitle,textContent} = result;
		recommends.splice(index,1,{
			id,
			title,
			imgUrl,
			imgAlt,
			imgTitle,
			textContent
		});
		fs.writeFileSync('./data/requiredJSON/recommends.json',JSON.stringify(recommends));
		ctx.body = {
			status:true
		}
	}else{
		ctx.body = {
			status:false
		}
	}
};

//获取友情链接
exports.getFriendLinks = async ctx=>{
	let friendLinks = GetFriendLinks();
	ctx.body = {
		data:friendLinks
	}
};
//修改友情链接
exports.changeFriendLink = async ctx=>{
	let {index,name,href,canSpider} = ctx.request.body;
	let friendLinks = GetFriendLinks();
	friendLinks.splice(index,1,{
		name,
		href,
		canSpider
	});
	fs.writeFileSync('./data/requiredJSON/friendLinks.json',JSON.stringify(friendLinks));
	ctx.body = {
		status:true
	}
};
//添加链接
exports.addFriendLink = async ctx=>{
	let {name,href} = ctx.request.body;
	let friendLinks = GetFriendLinks();
	friendLinks.push({
		name,
		href,
		canSpider:false
	});
	fs.writeFileSync('./data/requiredJSON/friendLinks.json',JSON.stringify(friendLinks));
	ctx.body = {
		status:true
	}
};
//删除链接
exports.delFriendLink = async ctx=>{
	let {index} = ctx.request.body;
	let friendLinks = GetFriendLinks();
	friendLinks.splice(index,1);
	fs.writeFileSync('./data/requiredJSON/friendLinks.json',JSON.stringify(friendLinks));
	ctx.body = {
		status:true
	}
};
//获取底部信息
exports.getFooterInfo = async ctx=>{
	let footerInfo = GetFooterInfo();
	ctx.body = {
		data:footerInfo
	}
};
//修改底部信息
exports.changeFooterInfo = async ctx=>{
	let {qqNumber,imgUrl,recordNumber,copyRight} = ctx.request.body;
	let footerInfo = {
		qqNumber,
		copyRight,
		recordNumber,
		imgUrl
	};
	fs.writeFileSync('./data/requiredJSON/connectInfo.json',JSON.stringify(footerInfo));
	ctx.body = {
		status:true
	}
};
//获取seo信息
exports.getSeoInfo = async ctx=>{
	let seoInfo = GetSeoInfo();
	ctx.body = {
		data:seoInfo
	}
};
//修改seo信息
exports.changeSeoInfo = async ctx=>{
	let {title,description,keywords} = ctx.request.body;
	let data = {
		title,
		description,
		keywords
	};
	fs.writeFileSync('./data/requiredJSON/seoInfo.json',JSON.stringify(data));
	ctx.body = {
		status:true
	}
};
